package com.dahua.messagesyncbj.controller;

import com.dahua.messagesyncbj.config.result.Exception.CommonException;
import com.dahua.messagesyncbj.config.result.ResultVO;
import com.dahua.messagesyncbj.entity.Fault;
import com.dahua.messagesyncbj.pojo.AlarmData;
import com.dahua.messagesyncbj.pojo.FaultData;
import com.dahua.messagesyncbj.pojo.SbData;
import com.dahua.messagesyncbj.pojo.XdcDta;
import com.dahua.messagesyncbj.service.EquipmentService;
import com.dahua.messagesyncbj.service.FaultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;

import static com.dahua.messagesyncbj.business.Constants.CHINESE_NUMBER;

@Slf4j
@RestController
@RequestMapping("/bjdt/historyAlarmRecord")
public class EquipmentController {
    @Resource
    private FaultService faultService;
    @Resource
    private EquipmentService equipmentService;

    //预警类
    @PostMapping("/pushDataAlarm")
    public ResultVO alarm(@RequestBody AlarmData data) {
        if (data.getEquipmentId() == null || data.getStartTime() == null) {
            throw new CommonException("请求参数错误", "设备编号不能为空，发生时间不能为空");
        }
        Fault fault = new Fault(data.getEquipmentId(),
                data.getAlarmLevel(),
                data.getAlarmDescribe(),
                Timestamp.valueOf(data.getStartTime()),
                data.getAlarmContent(),
                data.getSolution());
        //设置预警测点，目前只有主风机和射流风机
        fault.setMeasurementId(data.getMeasurementId());
        faultService.insertFault(fault, "A");
        return ResultVO.success("ok");
    }

    //故障类
    @PostMapping("/pushDataFault")
    public ResultVO alert(@RequestBody FaultData data) {
        if (data.getEquipmentId() == null || data.getStartTime() == null) {
            throw new CommonException("请求参数错误", "设备编号不能为空，发生时间不能为空");
        }
        Fault fault = new Fault(data.getEquipmentId(),
                data.getFaultLevel(),
                data.getFaultDescribe(),
                Timestamp.valueOf(data.getStartTime()),
                data.getFaultContent(),
                data.getSolution());
        faultService.insertFault(fault, "B");
        return ResultVO.success("ok");
    }

    @PostMapping("/pushDataAlarmHandle")
    public ResultVO recoverAlarm(@RequestBody AlarmData data) {
        if (data.getEquipmentId() == null || data.getEndTime() == null) {
            throw new CommonException("请求参数错误", "设备编号不能为空，结束时间不能为空");
        }
        Fault fault = new Fault();
        fault.setManufactureId(data.getEquipmentId());
        fault.setMeasurementId(data.getMeasurementId());
        fault.setLevel(data.getAlarmLevel());
        fault.setEndTime(Timestamp.valueOf(data.getEndTime()));
        fault.setDescribe(data.getAlarmDescribe());
        fault.setContent(data.getAlarmContent());
        fault.setSensorType("A");
        faultService.recover(fault);
        return ResultVO.success("ok");
    }

    @PostMapping("/pushDataFaultHandle")
    public ResultVO recoverFault(@RequestBody FaultData data) {
        if (data.getEquipmentId() == null || data.getEndTime() == null) {
            throw new CommonException("请求参数错误", "设备编号不能为空，结束时间不能为空");
        }
        Fault fault = new Fault();
        fault.setManufactureId(data.getEquipmentId());
        fault.setLevel(data.getFaultLevel());
        fault.setEndTime(Timestamp.valueOf(data.getEndTime()));
        fault.setDescribe(data.getFaultDescribe());
        fault.setContent(data.getFaultContent());
        fault.setSensorType("B");
        faultService.recover(fault);
        return ResultVO.success("ok");
    }

    @PostMapping("/pushXdcData")
    public ResultVO pushXdcData(@RequestBody XdcDta data) {
        if (data.getState() == 1 && data.getStartTime() == null) {
            throw new CommonException("请求参数错误", "告警信息开始时间不能为空");
        }
        if (data.getState() == 0 && data.getEndTime() == null) {
            throw new CommonException("请求参数错误", "恢复信息结束时间不能为空");
        }
        Fault fault = new Fault();
        switch (data.getDeviceType()) {
            case 1: {
                fault.setEquipmentType("UPS");
                break;
            }
            case 2: {
                fault.setEquipmentType("EPS");
                break;
            }
            case 3: {
                fault.setEquipmentType("XDC");
                break;
            }
            case 4: {
                fault.setEquipmentType("GW");
                break;
            }
        }
        fault.setManufactureId(data.getSn());
        fault.setContent(data.getMessage());
        fault.setLevel(data.getLevel());
        if(data.getWarnType() == 2){
            StringBuffer buffer = new StringBuffer();
            buffer.append(data.getLevel() + "级预警");
            if (!(data.getBatNo() == 0)){
                buffer.append(":"+data.getBatNo()+"号电池");
            }
            fault.setDescribe(buffer.toString());
            fault.setSensorType("A");
        }else if(data.getWarnType() == 1){
            StringBuffer buffer = new StringBuffer();
            buffer.append(data.getLevel() + "级故障");
            if (!(data.getBatNo() == 0)){
                buffer.append(":"+data.getBatNo()+"号电池");
            }
            fault.setDescribe(buffer.toString());
            fault.setSensorType("B");
        }

        switch (data.getState()){
            case 1:{
                fault.setStartTime(Timestamp.valueOf(data.getStartTime()));
                if(data.getWarnType() == 2){
                    faultService.insertFault(fault, "A");
                }else if(data.getWarnType() == 1){
                    faultService.insertFault(fault, "B");
                }
                break;
            }
            case 0:{
                fault.setEndTime(Timestamp.valueOf(data.getEndTime()));
                faultService.recover(fault);
                break;
            }
        }
        return ResultVO.success("ok");
    }


    @PostMapping("/pushSbData")
    public ResultVO pushSbData(@RequestBody SbData data) {
        Fault fault = new Fault();
        fault.setDescribe(data.getAlarmLevel());
        fault.setManufactureId(data.getEquipmentId());
        fault.setStartTime(Timestamp.valueOf(data.getStartTime()));
        fault.setContent(data.getAlarmId() + " " + data.getAlarmContent() + " " + "负责人：" + data.getManager() + " " + data.getManagerPhone());

        Integer integer = CHINESE_NUMBER.get(data.getAlarmLevel().charAt(0));
        fault.setLevel(integer);


        if (data.getAlarmSts().equals("001") || data.getAlarmSts().equals("011")){
            faultService.insertFault(fault,"A");
        }else{
            fault.setEndTime(Timestamp.valueOf(LocalDateTime.now()));
            faultService.recover(fault);
        }

        return ResultVO.success("ok");
    }
}
